package info.nixbear.tgcms.admin.client.view;

import info.nixbear.tgcms.admin.client.activity.PageEditActivity;
import info.nixbear.tgcms.admin.client.plupload.File;
import info.nixbear.tgcms.admin.client.plupload.Plupload;
import info.nixbear.tgcms.admin.client.plupload.PluploadBuilder;

import com.google.gwt.cell.client.TextCell;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;

public class PageEditView extends Composite {
	
	private static PageEditViewUiBinder uiBinder = GWT.create(PageEditViewUiBinder.class);
	interface PageEditViewUiBinder extends UiBinder<Widget, PageEditView> { }

	public static final String browseButtonId = "browse";
	
	PageEditActivity pageEditActivity;
	@UiField PageEditor editor;
	@UiField Button browse;
	@UiField CellTable<File> filesToUploadTable;
	public Plupload plupload;

	public PageEditView() {
		initWidget(uiBinder.createAndBindUi(this));
		initColumns();
		browse.getElement().setId(browseButtonId);
	}

	@Override
	protected void onLoad() {
		PluploadBuilder builder = new PluploadBuilder();
		builder.runtime("html5");
		builder.useQueryString(false);
		builder.multipart(true);
		builder.browseButton(PageEditView.browseButtonId);
		builder.listener(this.pageEditActivity);
		plupload = builder.create();
		plupload.init();
	}
	
	@Override
	protected void onUnload() {
		plupload.destroy();
		plupload = null;
	}

	@UiHandler("save")
	void onSave(ClickEvent event) {
		this.pageEditActivity.onSave(event);
	}
	
	@UiHandler(value={"cancel","back"})
	void onCancel(ClickEvent event) {
		this.pageEditActivity.onCancel(event);
	}
	
	@UiHandler("upload")
	void onUplaod(ClickEvent event) {
		this.pageEditActivity.onUplaod(event);
	}

	public PageEditor getEditor() {
		return editor;
	}

	public PageEditActivity getPageEditActivity() {
		return pageEditActivity;
	}

	public void setPageEditActivity(PageEditActivity pageEditActivity) {
		this.pageEditActivity = pageEditActivity;
	}

	public CellTable<File> getFilesToUploadTable() {
		return filesToUploadTable;
	}

	private void initColumns() {
		filesToUploadTable.addColumn(new Column<File, String>(new TextCell()) {
			@Override
			public String getValue(File object) {
				return object.getName();
			}
		}, "Name");
		
		filesToUploadTable.addColumn(new Column<File, String>(new TextCell()) {
			@Override
			public String getValue(File object) {
				return object.getSize() + " B";
			}
		}, "Size");
		filesToUploadTable.setColumnWidth(1, "100px");
		
		filesToUploadTable.addColumn(new Column<File, String>(new TextCell()) {
			@Override
			public String getValue(File object) {
				return object.getPercent() + " %";
			}
		}, "Progress");
		filesToUploadTable.setColumnWidth(2, "100px");
	}

}
